/*
 * Copyright (C) 2010 Demura Igor
 * DLisp project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dlisp.ide;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

public class EditPanel extends JPanel implements ActionListener
{
    public EditPanel()
    {
        super(new BorderLayout());

        JToolBar stdToolBar = createStdToolBar();
        add(stdToolBar, BorderLayout.PAGE_START);

        codeEditor = new CodeEditor();

        JScrollPane scrollPane = new JScrollPane(codeEditor);
        add(scrollPane, BorderLayout.CENTER);

        setPreferredSize(new Dimension(640, 400));
    }

    JToolBar createStdToolBar()
    {
        JToolBar stdToolBar = new JToolBar("Standard");
        stdToolBar.add(createTextToolBarBtn(ACTION_OPEN, "Open...", "Open file"));
        stdToolBar.add(createTextToolBarBtn(ACTION_SAVE, "Save", "Save file"));
        // stdToolBar.setSize(250, 52);
        return stdToolBar;
    }

    JButton createTextToolBarBtn(String action, String name, String tooltip)
    {
        JButton btn = new JButton();
        btn.setActionCommand(action);
        btn.addActionListener(this);
        btn.setToolTipText(tooltip);
        btn.setText(name);
        // Dimension size = new Dimension(TOOLBAR_BTN_SIZE.width,
        // btn.getMinimumSize().height);
        Dimension size = TOOLBAR_BTN_SIZE;
        btn.setMinimumSize(size);
        btn.setMaximumSize(size);
        return btn;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        if (cmd.equals(ACTION_OPEN))
        {
            appendLine("Open");
        }
        else if (cmd.equals(ACTION_SAVE))
        {
            appendLine("Save");
        }
        else
        {
            appendLine("Unknown command");
        }
    }

    void appendLine(String text)
    {
        codeEditor.setText(codeEditor.getText() + text + "\n");
        // codeEditor.setCaretPosition(codeEditor.getDocument().getLength());
    }

    static final String ACTION_OPEN = "File/Open";
    static final String ACTION_SAVE = "File/Save";
    static Dimension TOOLBAR_BTN_SIZE = new Dimension(64, 48);

    CodeEditor codeEditor;
}
